﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Apparat.Renderables;

namespace Apparat
{
  public partial class RenderControl : UserControl
  {
    public RenderControl()
    {
      InitializeComponent();
      this.MouseWheel += new MouseEventHandler(RenderControl_MouseWheel);
      FrameCounter.Instance.FPSCalculatedEvent += new FrameCounter.FPSCalculatedHandler(Instance_FPSCalculatedEvent);
    }

    delegate void setFPS(string fps);
    void Instance_FPSCalculatedEvent(string fps)
    {
      if (this.InvokeRequired)
      {
        setFPS d = new setFPS(Instance_FPSCalculatedEvent);
        this.Invoke(d, new object[] { fps });
      }
      else
      {
        this.DebugTextLabel.Text = fps;
      }
    }



    public void Init()
    {
      DeviceManager.Instance.Init(this);
      ShaderManager.Instance.Init();
      ShaderManager.Instance.LoadShaders();
      RenderManager.Instance.Init();

      Grid grid = new Grid(50, 1.0f);
      //SuperEllipsoidCol se = new SuperEllipsoidCol(32, 0.5f, 0.2f, 0.2f);

      //Scene.Instance.addRenderObject(se);
      Scene.Instance.AddRenderObject(grid);
    }

    public void ShutDown()
    {
      RenderManager.Instance.ShutDown();
      ShaderManager.Instance.ShutDown();
      DeviceManager.Instance.ShutDown();
    }

    private void RenderControl_MouseUp(object sender, MouseEventArgs e)
    {
      CameraManager.Instance.currentCamera.MouseUp(sender, e);
    }

    private void RenderControl_MouseDown(object sender, MouseEventArgs e)
    {
      CameraManager.Instance.currentCamera.MouseDown(sender, e);
    }

    private void RenderControl_MouseMove(object sender, MouseEventArgs e)
    {
      CameraManager.Instance.currentCamera.MouseMove(sender, e);
    }

    void RenderControl_MouseWheel(object sender, MouseEventArgs e)
    {
      CameraManager.Instance.currentCamera.MouseWheel(sender, e);
    }

    private void RenderControl_KeyUp(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.F1)
      {
        CameraManager.Instance.CycleCameras();
      }
      else if (e.KeyCode == Keys.F2)
      {
        RenderManager.Instance.SwitchSyncInterval();
      }
      else if (e.KeyCode == Keys.F12)
      {
        RenderManager.Instance.makeScreenshot = true;
      }

      CameraManager.Instance.currentCamera.KeyUp(sender, e);
    }

    private void RenderControl_KeyPress(object sender, KeyPressEventArgs e)
    {
      CameraManager.Instance.currentCamera.KeyPress(sender, e);
    }

    private void RenderControl_KeyDown(object sender, KeyEventArgs e)
    {
      CameraManager.Instance.currentCamera.KeyDown(sender, e);
    }

    private void RenderControl_Resize(object sender, EventArgs e)
    {
      RenderManager.Instance.resize = true;
    }

  }
}
